package com.alibaba.citrus.cr.mn.order.center.facade.function.common;

import cn.hutool.core.date.StopWatch;
import com.alibaba.cz.base.tool.log.Log;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangxiaorui
 * @ClassName OrderBizSubmitService
 * @description: TODO
 * @date 2023/05/16 11:28
 * @version: 1.0
 */
public interface OrderBizSubmitService<BizInput, AtomInput, BizOutput> {

    Log log = Log.getLogger(OrderBizSubmitService.class);

    default List<BizOutput> batchSubmit(BizInput request) {
        StopWatch stopWatch = new StopWatch("OrderBizSubmitService");
        try {
            // 1.0 参数预处理，订单头公共属性 同步更新至 订单明细
            stopWatch.start("preHandleRequestParams");
            preHandleRequestParams(request);
            stopWatch.stop();

            // 1.5 参数预处理，订单头公共属性 同步更新至 订单明细
            stopWatch.start("paramsInvalidCheck");
            paramsInvalidCheck(request);
            stopWatch.stop();

            // 2.0 业务校验-防腐适配处理 以及基础数据转换
            stopWatch.start("assembleBizBaseInfo");
            List<BizOutput> responses = assembleBizBaseInfo(request);
            stopWatch.stop();

            // 3.0 商业Request->原子Request
            stopWatch.start("convertAtomRequest");
            AtomInput atomRequest = convertAtomRequest(request);
            stopWatch.stop();

            // 4.0 调用订单原子能力
            stopWatch.start("processAtomAbility");
            processAtomAbility(atomRequest, responses);
            stopWatch.stop();

            // 5.0 打印链路耗时日志
//            printExecConsumerTime();

            return responses;
        } catch (Exception e) {
            log.error("OrderBizSubmitService#batchSubmit error", e);
            throw e;
        } finally {
            if (stopWatch.isRunning()) stopWatch.stop();
            log.info("OrderBizSubmitService#batchSubmit cost time:{}", stopWatch.prettyPrint(TimeUnit.MILLISECONDS));
        }
    }

    void preHandleRequestParams(BizInput bizBatchSubmitRequest);

    void paramsInvalidCheck(BizInput bizBatchSubmitRequest);

    List<BizOutput> assembleBizBaseInfo(BizInput bizOrderSubmitList);

    AtomInput convertAtomRequest(BizInput bizBatchSubmitRequest);

    void processAtomAbility(AtomInput atomSubmitRequest, List<BizOutput> responses);

    void printExecConsumerTime();
}
